package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.MyBusinessException;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentFacilityMapper;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

   @Autowired
   ApartmentInfoMapper apartmentInfoMapper;
   @Autowired
   ApartmentLabelService apartmentLabelService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private LabelInfoService labelInfoService;
    @Autowired
    private FacilityInfoService facilityInfoService;
    @Autowired
    private FeeValueService feeValueService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;
    @Autowired
    private RoomInfoService roomInfoService;
    @Override
    public Page<ApartmentItemVo> getApartmentItemVoPage(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
       return apartmentInfoMapper.getApartmentItemVoPage(page,queryVo);


    }

    @Override
    public ApartmentDetailVo getApartmentDetailVo(Long id) {
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
//        @Schema(description = "图片列表")
//        private List<GraphVo> graphVoList;
      List<GraphVo> graphVoList=graphInfoService.getGraphVoByApartmentId(id);
//        @Schema(description = "标签列表")
//        private List<LabelInfo> labelInfoList;
//     因为一个公寓可以对应多个标签，数据库的设计是apartment_label和labelInfo进行关联
       List<LabelInfo>labelInfoList= labelInfoService.getLabelListByApartmentId(id);
//        @Schema(description = "配套列表")
//        private List<FacilityInfo> facilityInfoList;
//      和上面的标签同理
        List<FacilityInfo> facilityInfos=facilityInfoService.getFacilityInfoByApartmentId(id);
//        @Schema(description = "杂费列表")
//        private List<FeeValueVo> feeValueVoList;
        //这个是三表联查
        //feeKey对应多个feeValue
        //公寓apartment_fee_value对应多个fee_value
        List<FeeValueVo> feeValueVoList=feeValueService.getFeeValueVoByApartmentId(id);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfos);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;
    }

    @Override
    public void saveApartmentSubmitVo(ApartmentSubmitVo apartmentSubmitVo) {
      //第一步先获取公寓id,用id来判断是删除还是修改
      Long id=apartmentSubmitVo.getId();

      //前端传过来的是当前省的id值，和之前未修改的城市值。所以需要修改当前城市值所对应的id值
        ProvinceInfo one = provinceInfoService.getOne(new LambdaQueryWrapper<ProvinceInfo>().eq(ProvinceInfo::getId, apartmentSubmitVo.getProvinceId()));
        apartmentSubmitVo.setProvinceName(one.getName());
        CityInfo one1 = cityInfoService.getOne(new LambdaQueryWrapper<CityInfo>().eq(CityInfo::getId, apartmentSubmitVo.getCityId()));
          apartmentSubmitVo.setCityName(one1.getName());
        DistrictInfo one2 = districtInfoService.getOne(new LambdaQueryWrapper<DistrictInfo>().eq(DistrictInfo::getId, apartmentSubmitVo.getDistrictId()));
        apartmentSubmitVo.setDistrictName(one2.getName());
        //先将公寓传入得到id值
        this.saveOrUpdate(apartmentSubmitVo);
      //第二步如果为null就根据id删除所有值
        if(id!=null)
        {

            //删除公寓配套
            apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
           //删除公寓配套标签
           apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
           //删除公寓配套图片
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.APARTMENT).eq(GraphInfo::getItemId,id));
            //删除公寓杂费
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));
        }

          //第三步直接对传过来的数据进行添加

        List<ApartmentFacility> facilityIds=new ArrayList<>();

        apartmentSubmitVo.getFacilityInfoIds().stream().forEach(f->{
           ApartmentFacility apartmentFacility= ApartmentFacility.builder()
                   .apartmentId(apartmentSubmitVo.getId())
                   .facilityId(f).build();
            facilityIds.add(apartmentFacility);
        });

        apartmentFacilityService.saveBatch(facilityIds);

        List<ApartmentLabel> apartmentLabels=new ArrayList<>();
        apartmentSubmitVo.getLabelIds().stream().forEach(
                f->{
                    ApartmentLabel apartmentLabel=ApartmentLabel.builder().apartmentId(apartmentSubmitVo.getId())
                            .labelId(f).build();
                   apartmentLabels.add(apartmentLabel);
                }
        );
        apartmentLabelService.saveBatch(apartmentLabels);


        List<ApartmentFeeValue> apartmentFeeValues=new ArrayList<>();
        apartmentSubmitVo.getFeeValueIds().stream().forEach(
                f->{
                    ApartmentFeeValue apartmentFeeValue=ApartmentFeeValue.builder()
                            .apartmentId(apartmentSubmitVo.getId())
                            .feeValueId(f).build();
                    apartmentFeeValues.add(apartmentFeeValue);
                }
        );
        apartmentFeeValueService.saveBatch(apartmentFeeValues);


        List<GraphInfo> graphInfos=new ArrayList<>();
        apartmentSubmitVo.getGraphVoList().stream().forEach(
                f->{
                    GraphInfo graphInfo=new GraphInfo();
                    graphInfo.setItemId(apartmentSubmitVo.getId());
                    graphInfo.setItemType(ItemType.APARTMENT);
                    graphInfo.setUrl(f.getUrl());
                    graphInfo.setName(f.getName());
                  graphInfos.add(graphInfo);
                }
        );

        graphInfoService.saveBatch(graphInfos);



    }

    @Override
    public void removeByApartmentId(Long id) {

        Long one = roomInfoService.count(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, id));
        if(one>0)
        {
            throw new MyBusinessException(10000,"公寓不能有房间，无法删除");
        }

         this.removeById(id);
        //删除公寓配套
        apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
        //删除公寓配套标签
        apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
        //删除公寓配套图片
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.APARTMENT).eq(GraphInfo::getItemId,id));
        //删除公寓杂费
        apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));
    }
}




